#include "Zombie.h"

#include "stdio.h"
#include "MapSlot.h"

Zombie* Zombie::create(unsigned int	key,CCPoint startPoint,CCPoint endPoint)
{
	const CCRect SIZE_128 = CCRect::CCRect(0,0,128,128);

	char* imgUri = "images\\Zombies\\ZombieEdit\\Zombie0.png";
	Zombie* pZombie = new Zombie(key,startPoint,endPoint);
	pZombie->initWithSpriteFrame(CCSpriteFrame::create(imgUri,SIZE_128));
	pZombie->autorelease();
	pZombie->StartMove();
	return pZombie;
}

void Zombie::StartMove()
{

	this->setAnchorPoint(RectLayout::Center());
	this->setVisible(true);
	this->setPosition(m_startPoint);
	CCActionInterval* actionTo = CCMoveBy::create(4, ccp(-MapSlot::WIDTH,0));

	const unsigned int ZOMBIE_FRAME_COUNT = 22;
	const CCRect RECT_SIZE_128 = CCRect::CCRect(0,0,128,128);

	CCArray* animFrames = CCArray::createWithCapacity(ZOMBIE_FRAME_COUNT);
	for (unsigned int i=0;i<ZOMBIE_FRAME_COUNT;i++)
	{
		char* filename = new char[50];
		sprintf(filename,"images\\Zombies\\ZombieEdit\\Zombie%d.png",i);
		animFrames->addObject(CCSpriteFrame::create(filename,RECT_SIZE_128));
	}

	CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames, 0.3f);

	this->runAction(CCRepeatForever::create( CCAnimate::create(animation) ) );
	this->runAction( CCRepeatForever::create(actionTo) );
}

void Zombie::ReStartMove()
{
	this->m_liveState = kNormalZombie;
	this->stopAllActions();
	this->StartMove();
}

cocos2d::CCPoint Zombie::GetLocation()
{
	return this->getPosition();
}

void Zombie::SetInValid()
{
	this->m_liveState = kDeadZombie;
	this->setVisible(false);
	this->stopAllActions();
}

CCRect Zombie::Rect()
{
	return CCRectMake(this->GetLocation().x, this->GetLocation().y, (this->getTextureRect().size.width)/2, (this->getTextureRect().size.height)/2);
}
